package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.lcb.manager.entity.Person;
import com.lcb.manager.repository.PersonRepository;
import com.lcb.manager.vo.Json;
import com.lcb.manager.vo.PersonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Controller
@RequestMapping(value = "/person")
public class PersonController {

    @Autowired
    private PersonRepository personRepository;

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(@RequestParam(defaultValue = "") String keyword, @RequestParam(defaultValue = "") String area, @RequestParam(defaultValue = "-1") Integer sex, @RequestParam int page, @RequestParam int limit) {
        Json result = new Json();
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Person> pageResult;
        if (keyword.isEmpty()) keyword = null;
        if (area.isEmpty()) area = null;
        if (sex == -1) sex = null;
        pageResult = personRepository.findByAllCondition(keyword, area, sex, pageable);
        personRepository.findAll();
        result.code = 0;
        result.count = (int) pageResult.getTotalElements();
        result.data = pageResult.getContent();
        return JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd");
    }

    @RequestMapping(value = "/get_one", method = RequestMethod.GET)
    public String getInfo(Model model, @RequestParam int id) {
        Person person = personRepository.findById(id).orElse(null);
        model.addAttribute("personInfo", PersonVo.fromEntity(person));
        return "_person_detail";
    }

    @RequestMapping(value = "/get_form")
    public String form(Model model, @RequestParam int id) {
        Person person = personRepository.findById(id).orElse(null);
        model.addAttribute("title", "个人信息");
        model.addAttribute("isEdit", true);
        if (person != null) {
            model.addAttribute("vo", PersonVo.fromEntity(person));
        }
        return "_person_info_form";
    }

    @ResponseBody
    @PostMapping("/edit")
    public String edit(
            @RequestParam("id") int id,
            @RequestParam("name") String name,
            @RequestParam("sex") int sex,
            @RequestParam("race") int race,
            @RequestParam("address") String address,
            @RequestParam("guardian") String guardian,
            @RequestParam("guardianContact") String guardianContact) throws IOException {
        Json result = new Json();
        Person person = personRepository.findById(id).orElse(null);
        if (person != null) {
            person.setName(name);
            person.setSex(sex);
            person.setRace(race);
            person.setAddress(address);
            person.setGuardian(guardian);
            person.setGuardianContact(guardianContact);
            personRepository.save(person);
        }
        result.code = 0;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }
}
